#!/usr/bin/env python

"""
A script to split-off E3SM's cime development and make it into a PR for ESMCI
"""

from standard_script_setup import *
from e3sm_cime_mgmt import *
from CIME.utils import expect

import sys, os, argparse

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
        usage="""\n{0} [--verbose]
OR
{0} --help
""".format(os.path.basename(args[0])),

description=description,

formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("repo", nargs="?",
                        help="Location of repo to use, default is based on current location")

    parser.add_argument("-r", "--resume", action="store_true",
                        help="Resume split after fixing conflicts")

    parser.add_argument("-a", "--abort", action="store_true",
                        help="Abort split")

    parser.add_argument("--squash", action="store_true",
                        help="Do the split as squashy as possible")

    parser.add_argument("--auto-conf", action="store_true",
                        help="Try to automatically resolve conflicts")

    args = CIME.utils.parse_args_and_handle_standard_logging_options(args, parser)

    expect(not (args.resume and args.abort), "Makes no sense to abort and resume")
    expect(not (args.abort and args.squash), "Makes no sense to abort and squash")

    return args.repo, args.resume, args.abort, args.squash, args.auto_conf

###############################################################################
def _main_func(description):
###############################################################################
    repo, resume, abort, squash, auto_conf = parse_command_line(sys.argv, description)

    if repo is not None:
        os.chdir(repo)

    if abort:
        abort_split()
    else:
        e3sm_cime_split(resume, squash=squash, auto_conf=auto_conf)

###############################################################################

if (__name__ == "__main__"):
    _main_func(__doc__)
